<template>
  <a-drawer
    :title="title"
    :closable="false"
    :maskClosable="false"
    width="60%"
    :visible="visible"
    :body-style="{ paddingBottom: '80px'}"
    @close="onClose"
  >
    <a-tabs v-model="tabkey">
      <a-tab-pane key="1" tab="中文"></a-tab-pane>
      <a-tab-pane key="2" tab="英文" :forceRender="true"></a-tab-pane>
    </a-tabs>
    <a-form-model 
      ref="ruleForm"
      :model="form" 
      :rules="rules" 
      :label-col="labelCol" 
      :wrapper-col="wrapperCol" 
      :key="visible"
    >
      <a-form-model-item label="年份" prop="year" >
        <a-select
          show-search
          v-model="form.mdate"
          placeholder="选择年份"
          option-filter-prop="children"
          style="width: 200px"
          :filter-option="(input,option) => option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0"
        >
          <a-select-option v-for="(item,index) of years" :value="item" :key="index">{{ item }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="年份标题" prop="title" v-if="tabkey == '1'">
        <a-input v-model="form.title" placeholder="请输入年份标题"></a-input>
      </a-form-model-item>
      <a-form-model-item label="英文年份标题" prop="titleEn" v-else>
        <a-input v-model="form.titleEn" placeholder="请输入英文年份标题"></a-input>
      </a-form-model-item>
      <a-form-model-item label="年份描述" prop="description" v-if="tabkey == '1'">
        <a-textarea
          v-model="form.description"
          placeholder="年份描述"
          :auto-size="{ minRows: 2, maxRows: 6 }"
        />
      </a-form-model-item>
      <a-form-model-item label="英文年份描述" prop="descriptionEn" v-else>
        <a-textarea
          v-model="form.descriptionEn"
          placeholder="英文年份描述"
          :auto-size="{ minRows: 2, maxRows: 6 }"
        />
      </a-form-model-item>
    </a-form-model>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 99999,
      }"
    >
      <a-button :style="{ marginRight: '8px' }" @click="onClose"> 关闭 </a-button>
      <a-button type="primary" @click="onSubmit"> 提交 </a-button>
    </div>
  </a-drawer>
</template>

<script>
import { addChronicle,updateChronicle } from '@/api/manage'
import { showMessage } from '@/utils/mixins'

export default {
  mixins: [showMessage],
  props: {
    parent: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      tabkey: '1',
      visible: false,
      title: '',
      labelCol: {span: 3},
      wrapperCol: {span: 21},
      form: {
        mdate: undefined,
        title: undefined,
        titleEn: undefined,
        description: undefined,
        descriptionEn: undefined,
        type: undefined
      },
      rules: {
        mdate: [{required: true, message: '年份不能为空', trigger: 'change'}],
        title: [{required: true, message: '新闻标题不能为空', trigger: 'blur'}],
        titleEn: [{required: true, message: '英文年份标题不能为空', trigger: 'blur'}],
        description: [{required: true, message: '新闻描述不能为空', trigger: 'blur'}],
        descriptionEn: [{required: true, message: '英文新闻描述不能为空', trigger: 'blur'}],
      },
      status: undefined,
      id: undefined,
    }
  },
  computed: {
    years() {
      const _nowYear = new Date().getFullYear()
      const _arrs = []
      for(let i = 0; i < 50; i++) {
        _arrs.push(_nowYear-i)
      }
      return _arrs
    }
  },
  methods: {
    // 提交
    onSubmit () {
      this.$refs.ruleForm.validate(vaild => {
        if(vaild) {
          this.form.type = 1
          if(this.status == 1) {
            addChronicle(this.form).then(res => {
              this.showMessage(res, () => {
                this.onClose()
                this.parent.getList()
              })
            })
          }else {
            updateChronicle({
              id: this.id,
              ...this.form
            }).then(res => {
              this.showMessage(res, () => {
                this.onClose()
                this.parent.getList()
              })
            })
          }
        }else {
          return false
        }
      })
    },
    // 关闭
    onClose() {
      for(const key in this.form) {
        this.form[key] = undefined
      }
      this.status = undefined
      this.visible = false
      this.id = undefined
      this.fileList = []
      this.tabkey = '1'
    }
  }
}
</script>